package com.ustc.hewei.shopmodule.serviceImpl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ustc.hewei.commonmodule.dto.Result;
import com.ustc.hewei.commonmodule.entity.Shop;
import com.ustc.hewei.commonmodule.entity.ShopType;
import com.ustc.hewei.commonmodule.mapper.ShopTypeMapper;
import com.ustc.hewei.commonmodule.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ustc.hewei.nettyrpc2_0.common.annotation.RPCService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.ustc.hewei.shopmodule.utils.Constants.CACHE_LIST_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hewei
 * @since 2022-10-05
 */
@Service
@RPCService(value = IShopTypeService.class)
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result getShopList() {
        String shopJson = stringRedisTemplate.opsForValue().get(CACHE_LIST_KEY);
        List<ShopType> shopTypeList;
        if (!StrUtil.isBlank(shopJson)) {
            shopTypeList = JSONUtil.toList(shopJson, ShopType.class);
        } else {
            shopTypeList = query().orderByAsc("sort").list();
            if (shopTypeList != null && !shopTypeList.isEmpty()) {
                stringRedisTemplate.opsForValue().set(CACHE_LIST_KEY, JSONUtil.toJsonStr(shopTypeList));
            }
        }
        return Result.ok(shopTypeList);
    }
}
